package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.yeyaomai.dksns.flowis.domain.ReferDomain;
import com.yeyaomai.dksns.flowis.domain.ReferPage;
import com.yeyaomai.dksns.flowis.service.ReferDomainService;
import com.yeyaomai.dksns.flowis.service.ReferPageService;
import com.yeyaomai.dksns.flowis.service.result.DynamicXml;
import com.yeyaomai.dksns.flowis.util.Globals;

@Controller
@RequestMapping("/domain/*")
public class RefererController {

	@Autowired
	private ReferDomainService referDomainService;

	@Autowired
	private ReferPageService referPageService;

	@RequestMapping(value = "/arrive")
	public ModelAndView getTodayCount(HttpServletRequest request)
			throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		String formDate = request.getParameter("formDate");
		String toDate = request.getParameter("toDate");
		String number = request.getParameter("number");
		String date = formDate;
		map.put("dateCount", 2);
		ModelAndView view = new ModelAndView("from-domain");
		view.addObject("formDate", formDate);
		view.addObject("toDate", toDate);
		if (formDate.equals(toDate)) {
			map.put("dateCount", 1);
		}
		toDate = Globals.FORMAT_TYPE_A.format(Globals.FORMAT_TYPE_A.parse(
				toDate).getTime()
				+ 1 * 24 * 3600 * 1000);

		map.put("fromReferDay", date);
		map.put("toReferDay", toDate);
		Page<ReferDomain> result = referDomainService.getDataPage(map,
				Integer.valueOf(number));
		view.addObject("result", result);
		return view;
	}

	/**
	 * 拼写饼状图
	 * 
	 * @param fromDate
	 * @param toDate
	 * @param response
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value = "/piexml")
	public void changeXml(@RequestParam("fromDate") String fromDate,
			@RequestParam("toDate") String toDate, HttpServletResponse response)
			throws IOException, ParseException {
		Map<String, String> referDomain = new HashMap<String, String>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<ReferDomain> domain = new ArrayList<ReferDomain>();
		map.put("dateCount", 2);
		String date = Globals.FORMAT_TYPE_A.format(Globals.FORMAT_TYPE_A.parse(
				toDate).getTime()
				+ 1 * 24 * 3600 * 1000);
		if (fromDate.equals(toDate)) {
			map.put("dateCount", 1);
		}
		map.put("fromReferDay", fromDate);
		map.put("toReferDay", date);
		map.put("startNumber", null);
		map.put("endNumber", null);
		domain = referDomainService.getReferDomainByDay(map);
		Integer othernumber = 0;
		for (int i = 0; i < domain.size(); i++) {
			ReferDomain referdomain = domain.get(i);
			if (i >= 5) {
				othernumber += referdomain.getVisitCount();
			} else {
				referDomain.put(referdomain.getDomainName(), referdomain
						.getVisitCount().toString());
			}
		}
		referDomain.put("其他", othernumber.toString());
		String xml = DynamicXml.getpie(referDomain, fromDate, toDate);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(xml);
	}

	@RequestMapping(value = "/referPage")
	public ModelAndView getTodayPage(HttpServletRequest request)
			throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		String formDate = request.getParameter("formDate");
		String toDate = request.getParameter("toDate");
		String number = request.getParameter("number");
		String date = formDate;
		map.put("dateCount", 2);
		ModelAndView view = new ModelAndView("from-page");
		view.addObject("formDate", formDate);
		view.addObject("toDate", toDate);
		if (formDate.equals(toDate)) {
			map.put("dateCount", 1);
		}
		toDate = Globals.FORMAT_TYPE_A.format(Globals.FORMAT_TYPE_A.parse(
				toDate).getTime()
				+ 1 * 24 * 3600 * 1000);

		map.put("fromReferDay", date);
		map.put("toReferDay", toDate);
		Page<ReferPage> result = referPageService.getDataPage(map,
				Integer.valueOf(number));
		view.addObject("result", result);
		return view;
	}
}
